//
//  ZXUIButtonExtension.swift
//  ExtensionTest
//
//  Created by ZX on 2025/2/6.
//

import UIKit

public extension UIButton {
    convenience init(_ image: UIImage) {
        self.init(type: .custom)
        self.setImage(image, for: .normal)
    }
    convenience init(_ text:String,_ font: UIFont,_ textColor: UIColor,_ bgImage: UIImage? = nil) {
        self.init(type: .custom)
        self.set(text, font, textColor, bgImage, .normal)
    }
    func set(_ text: String,_ font: UIFont,_ textColor: UIColor,_ bgImage: UIImage? = nil, _ state: UIControl.State) {
        self.setTitle(text, for: state)
        self.setTitleColor(textColor, for: state)
        self.titleLabel?.font = font
        self.setBackgroundImage(bgImage, for: state)
    }
}

public extension UIButton {
    convenience init(_ image: UIImage,_ target: Any? = nil, _ action: Selector? = nil, _ control: UIControl.Event = .touchUpInside) {
        self.init(type: .custom)
        self.setImage(image, for: .normal)
        if let target = target ,let action = action {
            self.addTarget(target, action: action, for: control)
        }
    }
    convenience init(_ text:String,_ font: UIFont,_ textColor: UIColor,_ bgImage: UIImage? = nil, _ target: Any? = nil, _ action: Selector? = nil, _ control: UIControl.Event = .touchUpInside) {
        self.init(type: .custom)
        self.setTitle(text, for: .normal)
        self.setTitleColor(textColor, for: .normal)
        self.titleLabel?.font = font
        self.setBackgroundImage(bgImage, for: .normal)
        if let target = target ,let action = action {
            self.addTarget(target, action: action, for: control)
        }
    }
}
